﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Claudia De Leon
////Date created: 04/08/2013
////ULTI-94693: : Location Business Rules-Add a Global Location in .NET https://ultidev/browse/ULTI-94693 
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules.Locations
{
    [TestFixture]
    [Author("Claudia De Leon", "claudia_deleon@ultimatesoftware.com")]
    [Category("Business Rules")]
    [Header("Config")]
    [Config(".NET")]
    public class AddGlobalLocation : MbUnitAutomatedTest
    {
        #region Setups / Teardowns
        UltiPro UltiPro;
        UltiProDatabaseContext Database;
        private readonly DateTime Date = DateTime.Today;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            #region Setup database ULTIPRO_CALENDAR
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("iLocation");
            Database.Company.AddRollBack("Loccomp");
            #endregion

            #region Login as System Admin USA-CANU for Web Payroll Company
            UltiPro.Pages.Login.LoginAs("merrillg", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
            #endregion

            #region Navigate to System Admin | Business Rules | Locations
            UltiPro.Macros.Navigate.ToBusinessRule("Locations");
            #endregion
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            Database.Company.RollBackDatabase();
            UltiPro.Browser.Quit();
        }

        [SetUp]
        public void SetUp()
        {
            #region Click on the Add button for a new location
            UltiPro.Pages.LocationSummary.ButtonBar.Click(button => button.Add);
            #endregion
        }
        #endregion

        [Test]
        public void RequiredFieldsOnAddPage()
        {
            #region GIVEN I am on the Add/Change Location page
            UltiPro.Pages.AddChgLocations.Is(title => title.PageHeaderLabel, "innerHTML", "Add/Change Location");
            #endregion
            
            #region WHEN I attempt to save the page
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion
            
            #region THEN I get an error message about correcting 1 highlighted field
            UltiPro.Pages.AddChgLocations.ErrorMessages.Has("Please correct the 1 highlighted field(s) before continuing");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void RequiredFieldsOnAddPageForAGlobalLocation()
        {
            #region GIVEN I select Australia as the location country
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.LocationCountryDropdown, "Australia", SelectOption.Text);
            #endregion
            
            #region WHEN I attempt to save the page
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion
            
            #region THEN then I get an error message about correcting highlighted fields:Code, Description, Trade name, Selected box, Address 1, City, State, Postal code
            UltiPro.Pages.AddChgLocations.ErrorMessages.Has("Please correct the 7 highlighted field(s) before continuing");
            //Updated Pysical Address fields required: https://ultidev/browse/ULTI-106367
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void NoDuplicateCodeOnAddPageForAGlobalLocation()
        {
            #region GIVEN I select Mexico as the location country
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.LocationCountryDropdown, "Mexico", SelectOption.Text);
            #endregion
            
            #region WHEN I set the Code to TJ
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.CodeTextbox, "TJ");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion
            
            #region THEN then I get an error message about code uniqueness
            UltiPro.Pages.AddChgLocations.ErrorMessages.Has("Code must be unique. Code TJ already exists.");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void EditableFieldsOnAddPage()
        {
            #region GIVEN that I am on the Add/Change Location page for a new location
            UltiPro.Pages.AddChgLocations.Is(page => page.PageHeaderLabel, "innerHTML", "Add/Change Location");
            #endregion
            #region WHEN I select Brazil as the location country
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.LocationCountryDropdown, "Brazil", SelectOption.Text);
            #endregion
            
            #region THEN the following fields are editable on the Details section: Code, Description, Trade name, As of
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.CodeTextbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.DescriptionTextbox.EnglishTextBox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.TradeNameTextbox.EnglishTextBox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.Enabled(label => label.AsOfLabel);
            #endregion

            #region AND Physical Address section: Address 1, Address 2, Address 3, Address 4, City, State/Province, Postal code
            UltiPro.Pages.AddChgLocations.Is(page => page.LocationCountryValue, "innerHTML", "Brazil");
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhyicalAddressLine1Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhyicalAddressLine2Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhyicalAddressLine3Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhyicalAddressLine4Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhyicalAddressCityTexbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhyicalAddressStateProvinceDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhyicalAddressZipPostalCodeTexbox);
            #endregion

            #region AND Address and Contact section: Address Line 1, Address Line 2, Address Line 3, Address Line 4, City, State Province, Zip/ Postal Code, Contact country, Contact name, Phone number, Extension
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.ContactCountryDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ContactAddressLine1Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ContactAddressLine2Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ContactAddressLine3Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ContactAddressLine4Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ContactCityTexbox);
            UltiPro.Pages.AddChgLocations.Enabled(dropdown => dropdown.ContactStateProvinceDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ContactZipPostalCodeTexbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ContactNameTexbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.PhoneNumberTextbox);
            UltiPro.Pages.AddChgLocations.Enabled(textbox => textbox.ExtensionTextbox);
            #endregion
            
            #region AND Companies using this tax location section: Companies mover box
            UltiPro.Pages.AddChgLocations.CompaniesOptionMover.Enabled(box => box.Source);
            UltiPro.Pages.AddChgLocations.CompaniesOptionMover.Enabled(box => box.Destination);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void SpecialCharsOnEditableFields()
        {
            #region GIVEN I select Belgium as the location country and complete the required fields
            // Details
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.LocationCountryDropdown, "Belgium", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.CodeTextbox, "GLBEL");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.DescriptionTextbox.EnglishTextBox, "Global Belgium");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.TradeNameTextbox.EnglishTextBox, "Global Belgium");
            //Physical Address
            CompleteRequiredFieldsForPhysicalAddress("Belgium ST", "Brusselas", "Belgium State", "123 anything");
            // Company
            UltiPro.Pages.AddChgLocations.Set(x => x.CompaniesOptionMover.Source, "Global Company Belgium", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Click(button => button.CompaniesOptionMover.ArrowRight);
            #endregion
            
            #region WHEN I enter: Special characters (%^&%*()_(*&^%$#), letters and numbers on Contact Address Line 1, City and phone number
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactAddressLine1Texbox, "&^*%$%%&^*");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactCityTexbox, "54545 Y&3w4");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.PhoneNumberTextbox, "-87677#c^&*+");
            #endregion
            
            #region AND attempt to save
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion
            
            #region THEN the pages saves the special characters on the fields
            UltiPro.Pages.AddChgLocations.Is(field => field.ContactAddressLine1Value, "innerHTML", "&^*%$%%&^*");
            UltiPro.Pages.AddChgLocations.Is(field => field.ContactCityValue, "innerHTML", "54545 Y&3w4");
            UltiPro.Pages.AddChgLocations.Is(field => field.PhoneNumberValue, "innerHTML", "87677#c^&+");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion

            #region Database check
            /*Database.Company.Verify("SELECT loccode, LocAddressLine1, LocAddressCity, LocPhoneMainNumber FROM location WHERE loccode = 'GLBEL' AND LocAddressLine1 = '&^*%$%%&^*' " +
                                     "AND LocAddressCity = '54545 Y&3w4' AND LocPhoneMainNumber = '-87677#c^&*+'", "LocationBusinessRules/SpecialCharsOnEditFields.xml");
            Database.Company.Verify("SELECT CmpCompanyName, CmpCoID FROM Loccomp JOIN Company on CmpCoID = lcpcoid WHERE lcpCode = 'GLBEL' ORDER BY lcpcode", "LocationBusinessRules/CompanyNameSelect.xml");*/
            #endregion
        }

        [Test]
        public void ResetFieldsOnNewGlobalLocationPage()
        {
            #region GIVEN I select Mexico as the location country
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.LocationCountryDropdown, "Mexico", SelectOption.Text);
            #endregion
            
            #region AND I enter text on required fields
            // Details
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.CodeTextbox, "GLMEX");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.DescriptionTextbox.EnglishTextBox, "Global Mexico");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.TradeNameTextbox.EnglishTextBox, "Global Mexico");
            // Physical Address
            CompleteRequiredFieldsForPhysicalAddress("Cuernavaca 28", "Xalapa", "Oaxaca ENG", "anything 123");
            // Company
            UltiPro.Pages.AddChgLocations.Set(x => x.CompaniesOptionMover.Source, "Global Co. Mexico", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Click(button => button.CompaniesOptionMover.ArrowRight);
            #endregion
            
            #region WHEN I click the reset button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Reset);
            #endregion
            
            #region THEN the only field available is the Location country under Details section
            UltiPro.Pages.AddChgLocations.Exists(header => header.DetailsHeader);
            UltiPro.Pages.AddChgLocations.Exists(label => label.LocationCountryLabel);
            UltiPro.Pages.AddChgLocations.Exists(dropdown => dropdown.LocationCountryDropdown);
            UltiPro.Pages.AddChgLocations.Is(text => text.LocationCountryDropdown, "innerHTML", "");
            UltiPro.Pages.AddChgLocations.NotExists(header => header.ContactInformationHeader);
            UltiPro.Pages.AddChgLocations.NotExists(header => header.PhysicalAddressHeader);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            #endregion
        }

        [Test]
        public void AddNewGlobalLocation()
        {
            #region GIVEN I select Italy as the location country
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.LocationCountryDropdown, "Italy", SelectOption.Text);
            #endregion

            #region AND Complete all the fields on the page
            // Details
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.CodeTextbox, "GLITA");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.DescriptionTextbox.EnglishTextBox, "Global Italy");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.TradeNameTextbox.EnglishTextBox, "Global Italy");
            UltiPro.Pages.AddChgLocations.Click(checkbox => checkbox.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.InactiveDate.Set(Date);
            // Physical Address
            CompleteRequiredFieldsForPhysicalAddress("Italy address ST", "Marcela", "Sicily ENG", "54/*()");
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressLine2Texbox, "Address line 2");
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressLine3Texbox, "Address line 3");
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressLine4Texbox, "Address line 4");
            // Contact Information
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.ContactCountryDropdown, "Mexico", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactAddressLine1Texbox, "6745 Ponce Lane");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactAddressLine2Texbox, "6746 Ponce Terr");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactAddressLine3Texbox, "6747 Ponce Ave");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactAddressLine4Texbox, "6748 Ponce ST");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactCityTexbox, "Marcela");
            UltiPro.Pages.AddChgLocations.Set(dropdown => dropdown.ContactStateProvinceDropdown, "Oaxaca ENG", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactZipPostalCodeTexbox, "26571");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ContactNameTexbox, "Rose Ann");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.PhoneNumberTextbox, "(786)258-3382");
            UltiPro.Pages.AddChgLocations.Set(textbox => textbox.ExtensionTextbox, "+25");
            // Company
            UltiPro.Pages.AddChgLocations.Set(x => x.CompaniesOptionMover.Source, "Global Company Italy", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Click(button => button.CompaniesOptionMover.ArrowRight);
            #endregion
            
            #region WHEN I click on the Save button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion
            
            #region THEN the page displays all the saved fields
            // Details
            UltiPro.Pages.AddChgLocations.Is(value => value.CodeValue, "innerHTML", "GLITA");
            UltiPro.Pages.AddChgLocations.Is(value => value.DescriptionValueEn, "innerHTML", "Global Italy");
            UltiPro.Pages.AddChgLocations.Is(value => value.TradeNameValueEn, "innerHTML", "Global Italy");
            UltiPro.Pages.AddChgLocations.Selected(checkbox => checkbox.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.Is(value => value.InactiveDateValue, "innerHTML", Date.ToString("MM/dd/yyyy"));
            // Physical Address
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressCountryValue, "innerHTML", "Italy");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressLine1Value, "innerHTML", "Italy address ST");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressLine2Value, "innerHTML", "Address line 2");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressLine3Value, "innerHTML", "Address line 3");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressLine4Value, "innerHTML", "Address line 4");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressCityValue, "innerHTML", "Marcela");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressStateProvinceValue, "innerHTML", "Sicily ENG");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressZipPostalCodeValue, "innerHTML", "54/");
            // Contact Information
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactCountryValue, "innerHTML", "Mexico");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactAddressLine1Value, "innerHTML", "6745 Ponce Lane");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactAddressLine2Value, "innerHTML", "6746 Ponce Terr");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactAddressLine3Value, "innerHTML", "6747 Ponce Ave");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactAddressLine4Value, "innerHTML", "6748 Ponce ST");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactCityValue, "innerHTML", "Marcela");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactStateProvinceValue, "innerHTML", "Oaxaca ENG");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactZipPostalCodeValue, "innerHTML", "26571");
            UltiPro.Pages.AddChgLocations.Is(value => value.ContactNameValue, "innerHTML", "Rose Ann");
            UltiPro.Pages.AddChgLocations.Is(value => value.PhoneNumberValue, "innerHTML", "7862583382");
            UltiPro.Pages.AddChgLocations.Is(value => value.ExtensionValue, "innerHTML", "+25");
            // Companies
            UltiPro.Pages.AddChgLocations.Is(value => value.CompaniesUsingThisTaxLocationList(1), "innerHTML", "Global Company Italy");
            #endregion
            
            #region AND the Menu Bar buttons are Back, Edit, Delete, Print, Help
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Edit);
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Delete);
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Back);
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Print);
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Help);
            UltiPro.Pages.AddChgLocations.ButtonBar.NotExists(button => button.Save);
            UltiPro.Pages.AddChgLocations.ButtonBar.NotExists(button => button.Reset);
            UltiPro.Pages.AddChgLocations.ButtonBar.NotVisible(button => button.Cancel);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion

            #region Database check
            /*Database.Company.Verify("SELECT loccode, LocDesc, LocName, LocIsInactive, LocAddressLine1, LocAddressLine2, " +
                                    "LocAddressLine3, LocAddressLine4, LocAddressCity, LocAddressState, LocAddressZipcode, LocContact, " +
                                    "LocPhoneMainNumber, LocPhoneMainExt FROM location WHERE loccode = 'GLITA' and LocDesc = 'Global Italy'" +
                                    " AND LocName = 'Global Italy' AND LocAddressLine1 = '6745 Ponce Lane' AND LocAddressLine2 = '6746 Ponce Terr' AND" +
                                     " LocAddressLine3 = '6747 Ponce Ave' AND LocAddressLine4 = '6748 Ponce ST' " +
                                     "AND LocAddressCity = 'Marcela' AND LocAddressState = 'OAXA' AND LocAddressZipcode ='26571' " +
                                     "AND LocContact = 'Rose Ann' AND LocPhoneMainNumber = '(786)258-3382' AND LocPhoneMainExt = '+25'", "LocationBusinessRules/AddGlobal.xml");
            Database.Company.Verify("SELECT CmpCompanyName, CmpCoID FROM Loccomp JOIN Company on CmpCoID = lcpcoid WHERE lcpCode = 'GLITA' ORDER BY lcpcode", "LocationBusinessRules/AddGlobalCompany.xml");
            var dbDate = Database.Company.GetValueAsDateTime("SELECT LocInactiveDate FROM location where loccode = 'GLITA'").ToShortDateString();
            Assert.AreEqual(Date.ToShortDateString(), dbDate);*/
            #endregion
        }

        //Note: Story https://ultidev/browse/ULTI-106036 covered in test case 
        //      ContactInformationLabels in AddCanadaLocation.cs file
        
        #region HELPERS
        /// <summary>
        /// Fill out all required fields for Physical address
        /// </summary>
        /// <param name="address1">Address 1</param>
        /// <param name="city">City</param>
        /// <param name="stateProvince">State/Province dropdown</param>
        /// <param name="postalCode">Postal Code</param>
        public void CompleteRequiredFieldsForPhysicalAddress(string address1, string city, string stateProvince, string postalCode)
        {
            // Added required Physical Address fields: https://ultidev/browse/ULTI-104632
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressLine1Texbox, address1);
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressCityTexbox, city);
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressStateProvinceDropdown, stateProvince, SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressZipPostalCodeTexbox, postalCode);
        }
        #endregion
    }
}
